package cn.bonoon.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.bonoon.kernel.menus.ModuleDefinition;
import cn.bonoon.kernel.menus.ModuleManager;
import cn.bonoon.kernel.menus.PartDefinition;
import cn.bonoon.kernel.menus.VersionDefinition;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.support.services.UserService;
import cn.bonoon.kernel.util.StringHelper;
import cn.bonoon.kernel.web.controllers.AbstractController;

@Controller
@RequestMapping("s/user")
public class HelpAboutController extends AbstractController{
	
	private String id;
	@Autowired
	protected ModuleManager moduleManager;
	
	@Autowired
	protected UserService userService;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		super.afterPropertiesSet();
		id = moduleManager.getId();
	}

	@RequestMapping("help")
	public String help(Model model){
		LogonUser user = getUser();
		model.addAttribute("id", id).addAttribute("attrs", "title='帮助文档' style='width:800px;height:550px;'");
		StringBuilder sb = new StringBuilder();
		_parseDoc(sb, moduleManager.module(), user);
		if(user.isSuper()){
			for(ModuleDefinition tmi : moduleManager.modules()){
				_parseDoc(sb, tmi, user);
			}
		}else{
			for(String mid : userService.accessableModules(user)){
				ModuleDefinition module = moduleManager.module(mid);
				if(null != module){
					_parseDoc(sb, module, user);
				}
			}
		}
		model.addAttribute("tabsHelpDocs", sb);
		return "help-doc";
	}

	private void _parseDoc(StringBuilder sb, ModuleDefinition mi, LogonUser user) {
		List<PartDefinition> pis = new ArrayList<>();
		for(PartDefinition pi : mi.getParts()){
			if(pi.isHelper() && pi.checked(user)){
				pis.add(pi);
			}
		}
		if(!pis.isEmpty()){
			sb.append("<div title='").append(mi.getName());
			if(pis.size() == 1){
				PartDefinition pi = pis.get(0);
				sb.append("' href='").append(pi.getUrl());
				String icon = pi.getIconCls();
				if(StringHelper.isNotEmpty(icon)){
					sb.append("' data-settings=\"iconClassname:'").append(icon);
				}
				sb.append("'\"></div>");
			}else{
				sb.append("'><div class='tabs-").append(id).append("-help-doc'  tabPosition='button'>");
				for(PartDefinition pi : pis){
					sb.append("<div title='").append(pi.getName()).append("' href='").append(pi.getUrl());
					String icon = pi.getIconCls();
					if(StringHelper.isNotEmpty(icon)){
						sb.append("' data-settings=\"iconClassname:'").append(icon);
					}
					sb.append("'\"></div>");
				}
				sb.append("</div></div>");
			}
		}
	}
	
	@RequestMapping("about")
	public String about(Model model){
		model.addAttribute("id", id).addAttribute("attrs", "title='关于...' style='width:400px;height:280px;'");
		StringBuilder thums = new StringBuilder(), des = new StringBuilder();
		parse(thums, des);
		if(getUser().isSuper()){
			int i = 1;
			String cpath = servletContext.getContextPath();
			for(VersionDefinition vi : moduleManager.getVersions()){
				vi.parse(cpath, i++, thums, des);
			}
		}
		model.addAttribute("thum", thums).addAttribute("des", des);
		return "help-about";
	}
	
	@Value("${current.version}")
	private String version;

	@Value("${about.image}")
	private String image;
	@Value("${about.company}")
	private String company;
	@Value("${about.time}")
	private String time;
	@Value("${about.team}")
	private String team;
	@Value("${about.des}")
	private String des;
	@Value("${about.phone}")
	private String phone;
	
	void parse(StringBuilder thums, StringBuilder des){
		String name = moduleManager.getName();
		thums.append("<a style='padding:4px;margin:8px;border-style:solid;border-width:0px;border-bottom-width:2px;' title='").append(name);
		thums.append("' href='#' id='thum-").append(id).append("-0'><img height='32' src='").append(image).append("'/></a>");
		
		des.append("<table style='width:100%;' border='0' cellpadding='0' cellspacing='0' id='des-thum-").append(id);
		des.append("-0'><tr><td rowspan='5' align='center' valign='top' style='width:80px;'><img src='").append(image);
		des.append("' height='64'/></td><td align='right' style='width:60px;'>版本：</td><td>").append(version);
		des.append("</td></tr><tr><td align='right'>名称：</td><td>").append(name).append("</td></tr>");
		if(StringHelper.isNotEmpty(company)){
			des.append("<tr><td align='right'>公司：</td><td>").append(company);
			if(StringHelper.isNotEmpty(phone)){
				des.append("(联系电话：").append(phone).append(')');
			}
			des.append("</td></tr>");
		}
		if(StringHelper.isNotEmpty(team)){
			des.append("<tr><td align='right'>团队：</td><td>").append(team);
			if(StringHelper.isNotEmpty(time)){
				des.append("(时间：").append(time).append(')');
			}
			des.append("</td></tr>");
		}
		if(StringHelper.isNotEmpty(this.des)){
			des.append("<tr><td align='right'>说明：</td><td>").append(this.des).append("</td></tr>");
		}
		des.append("</table>");
	}
}
